package com.wcgksoft.common.util.getdata;


import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     @author  : FlySand
 *     @e-mail  : 1156183505@qq.com
 *     @time    : 2019/5/8.
 *     @desc    : 解析助手
 * </pre>
 */
public class AnalysisHelper {

    /**
     * 解析成功与失败均通过接口回调
     *
     * @param datas    待解析数据
     * @param callBack 解析解决回调
     */
    public static void solution(byte[] datas, AnalysisCallBack callBack) {

        if (datas.length < 0 || callBack == null) {
            return;
        }

        List<String> source = new ArrayList<String>();

        for (int i = 0; i < datas.length; i++) {
            String hex = String.format("%02X", datas[i] & 0xff);
            source.add(hex);
        }
        Log.print(toListString(source, 1));

        if (source.size() < 16) {
            callBack.onFailure("帧格式错误");
            return;
        }
        JXCTPacket packet = new JXCTPacket();
        //帧头  帧头 2 字节，固定为 0xfe + 0xdc
        packet.setHeader(source.get(0) + source.get(1));
        if (!"FEDC".equals(packet.getHeader())) {
            callBack.onFailure("未识别的帧头:" + packet.getHeader());
            return;
        }
        packet.setVersion(source.get(2));
        packet.setDeviceNumber(getParseVal(source, 3, 6));
        packet.setSession(getParseVal(source, 9, 4));
        packet.setOrder(source.get(13));
        packet.setLength(getParseVal(source, 14, 2));
        int bodylength = Integer.parseInt(packet.getLength(), 16);
        if (source.size() < (16 + bodylength)) {
            callBack.onFailure("内容长度错误");
            return;
        }
        packet.setBody(source.subList(16, 16 + bodylength));
        packet.setEnd(getParseVal(source, 16 + bodylength, 1));
        callBack.onSuccess(packet);
    }

    /**
     * 获取设备发送数值
     *
     * @param hex
     * @return
     */
    public static double getAnalysisVal(String hex) {
        double val = 0;
        try {
            val = Long.parseLong(hex, 16);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        //使用补码的形式标示负数
        if (val > 16777215) {
            long f8 = 4294967296l;
            val = val - f8;
            if (val < -16777215l) {
                val = -16777215;
            }
        }
        return val;

    }

    /**
     * 获取解析值
     *
     * @param source 数据列
     * @param i      开始
     * @param i1     位置
     * @return 结果
     */
    public static String getParseVal(List<String> source, int i, int i1) {
        StringBuffer buffer = new StringBuffer();
        int len = i + i1;
        for (; i < len; i++) {
            buffer.append(source.get(i));
        }
        return buffer.toString();
    }

    /**
     * 转换list
     *
     * @param body  列表
     * @param space 间隔
     * @return
     */
    public static String toListString(List<String> body, int space) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < body.size(); i++) {
            buffer.append(body.get(i));
            if (i % space == space - 1) {
                buffer.append(" ");
            }
        }
        return buffer.toString();
    }
}
